﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LX.Web.Excel
{
    public class ImportHelper<T>
    {
        private T data;
        public ImportHelper()
        {
        }
        public ImportHelper(T data)
        {
            this.data = data;
        }
        public void SetData(T data)
        {
            this.data = data;
        }

        public void Fill(List<ImportItem> dicdata)
        {
            var propertys = typeof(T).GetProperties();
            foreach (var p in propertys)
            {
                if (dicdata.Any(t=>t.Name.ToLower()==p.Name.ToLower()))
                {
                    var item=dicdata.First(t=>t.Name.ToLower()==p.Name.ToLower());
                    //var value = item.Value;
                    TransValue(p, item);
                    filldata(p, item);
                }
                else
                {
                    var attrs = p.GetCustomAttributes(typeof(ImportAttribute), true);
                    if (attrs != null && attrs.Any())
                    {
                        ImportAttribute attr = (ImportAttribute)attrs[0];
                        var attrname = attr.Name;
                        if (dicdata.Any(t => t.Name.ToLower() == attrname.ToLower()))
                        {
                            var item = dicdata.First(t => t.Name.ToLower() == attrname.ToLower());
                            //var value = item.Value;
                            TransValue(p, item);
                            filldata(p, item);
                        }
                    }
                }
            }
        }

        protected virtual void TransValue(System.Reflection.PropertyInfo p, ImportItem item)
        {

        }

        private void filldata(System.Reflection.PropertyInfo p, ImportItem item)
        {
            object value = item.Value;
            if (value == null) return;
            if (string.IsNullOrWhiteSpace(value.ToString())) return;

            try
            {
                if (p.PropertyType == typeof(int))
                {
                    p.SetValue(data, Convert.ChangeType(value, typeof(int)), null);
                }
                else if (p.PropertyType == typeof(int?))
                {
                    p.SetValue(data, Convert.ChangeType(value, typeof(int?)), null);
                }
                else if (p.PropertyType == typeof(decimal))
                {
                    p.SetValue(data, Convert.ChangeType(value, typeof(decimal)), null);
                }
                else if (p.PropertyType == typeof(decimal?))
                {
                    p.SetValue(data, Convert.ChangeType(value, typeof(decimal?)), null);
                }
                else if (p.PropertyType == typeof(DateTime))
                {
                    p.SetValue(data, Convert.ChangeType(value, typeof(DateTime)), null);
                }
                else if (p.PropertyType == typeof(DateTime?))
                {
                    p.SetValue(data, Convert.ChangeType(value, typeof(DateTime?)), null);
                }
                else
                {
                    p.SetValue(data, value, null);
                }
            }
            catch
            {
                throw new Exception("录入数据格式不正确:第"+item.Row+"行,"+item.Name);
            }
        }
    }
}
